﻿using CPPEI.Coolzon.Core;
using CPPEI.Coolzon.Core.Data;
using CPPEI.Coolzon.Core.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Documents;

namespace CPPEI.Coolzon.BrowserAppHost
{
    public partial class LoadUserSolution : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //判断是否是登录用户
            if (!User.Identity.IsAuthenticated)
            {
                this.Response.Redirect("Login.aspx");
            }
            else
            {
                List<Solution> solutionList = new List<Solution>();
                string userId = User.Identity.Name;
                using (DbContext context = new DbContext())
                {
                    IUserRepository userrepository = RepositoryFactory.Create<IUserRepository>();
                    Core.Data.User user = userrepository.GetUser(context, userId);
                    //根据UserId获取权限列表
                    IAssignedPrivilegeRepository iAssignedPrivilegeRepository = RepositoryFactory.Create<IAssignedPrivilegeRepository>();
                    ISolutionRepository solutinRepository = RepositoryFactory.Create<ISolutionRepository>();
                    PagedList<AssignedPrivilege> assignedPrivilegeList = iAssignedPrivilegeRepository.GetAssignedPrivilegeByObjectId(context, user.Id);
                    IPrivilegeRepository privilegeRepository = RepositoryFactory.Create<IPrivilegeRepository>();
                    if (assignedPrivilegeList == null)
                    {
                        Response.Redirect("LoadUserSolutionError.aspx", false);
                        return;
                    }
                    //根据权限列表获取方案列表
                    for (int i = 0; i < assignedPrivilegeList.DataList.Count; i++)
                    {
                        Privilege privilege = privilegeRepository.GetPrivilege(context, assignedPrivilegeList.DataList[i].PrivilegeId);
                       Solution solution = solutinRepository.GetSolution(context, privilege.Code);
                        if (solution != null)
                            solutionList.Add(solution);
                    }
                }
                //根据权限列表，显示不同的页面信息
                if (solutionList.Count == 0)
                {
                    Response.Redirect("LoadUserSolutionError.aspx", false);
                }
                else if (solutionList.Count == 1)
                {
                    Response.Redirect("loadBrowserApp.aspx?solutionname=" + solutionList[0].Name);
                }
                else
                {
                    DDlSolution.DataSource = solutionList;
                    DDlSolution.DataTextField = "Name";
                    DDlSolution.DataValueField = "Name";
                    DDlSolution.DataBind();
                }
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (DDlSolution.SelectedValue != null)
            {
                Response.Redirect("loadBrowserApp.aspx?solutionname=" + DDlSolution.SelectedValue);
            }
        }
    }
}